package cn.kgc.tangcco.bean;

import java.util.Objects;

/**
 * @author 李昊哲
 * @Description 重写Object中的方法
 * @create 2020/8/24 上午10:19
 */
public class Person implements Cloneable {
    /**
     * 姓名
     */
    private String nickname;
    /**
     * 年龄
     */
    private Integer age;
    /**
     * 性别
     */
    private Integer gender;

    public Person() {
    }

    public Person(String nickname, Integer age, Integer gender) {
        this.nickname = nickname;
        this.age = age;
        this.gender = gender;
    }

    public String getNickname() {
        return nickname;
    }

    public void setNickname(String nickname) {
        this.nickname = nickname;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public Integer getGender() {
        return gender;
    }

    public void setGender(Integer gender) {
        this.gender = gender;
    }


    @Override
    public int hashCode() {
        // 参考值
        final int prime = 31;
        // 默认hashCode值
        int result = 1;

        result = prime * result + ((age == null) ? 0 : age);
        result = prime * result + ((gender == null) ? 0 : gender);
        result = prime * result + ((nickname == null) ? 0 : nickname.hashCode());

        // 返回hashCode值
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj){
            return true;
        }
        if (obj == null){
            return false;
        }
        if (getClass() != obj.getClass()){
            // 数据类型
            return false;
        }
        // 将参数Object对象赋值给当前类型的变量
        Person person = (Person) obj;
        if (age == null){
            if (person.age != null){
                return false;
            }
        }else if(!age.equals(person.age)){
            return false;
        }
        if (gender == null){
            if (person.gender != null){
                return false;
            }
        }else if(!gender.equals(person.gender)){
            return false;
        }
        if (nickname == null){
            if (person.nickname != null){
                return false;
            }
        }else if(!nickname.equals(person.nickname)){
            return false;
        }

        return true;
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        // 深克隆
        // return super.clone();
        // 浅克隆
        return this;
    }

    @Override
    public String toString() {
        return "Person{" + "nickname='" + nickname + "', " + "age=" + age + ", " + "gender=" + ((gender == 1) ? "'男'" : "'女'") + "}";
    }
}
